from rest_framework import serializers
from drf_haystack.serializers import HaystackSerializer
from .search_indexes import HouseListingIndex
from .models import HouseListing


class HouseListingIndexSerializer(HaystackSerializer):
    # 添加需要额外序列化的字段
    landlord = serializers.SerializerMethodField()
    orientation = serializers.CharField(source='object.orientation')
    deposit = serializers.DecimalField(source='object.deposit', max_digits=10, decimal_places=2, allow_null=True)
    id = serializers.IntegerField(source='object.id')
    class Meta:
        index_classes = [HouseListingIndex]
        fields = [
            "text",
            "suggestion",
            "id",
            "title",
            "address",
            "price",
            "area",
            "house_type",
            "status",
            "city",
            "district",
            "total_floor",
            "floor",
            "room_count",
            "bathroom_count",
            "decoration_type",
            "deposit",
            "orientation",
            "landlord",
            "description",
            "facilities"
        ]

    def get_landlord(self, obj):
        # 返回房东的用户名
        return obj.object.landlord.username